package com.example.rabbitmqdemo.dead_letter;

import com.example.rabbitmqdemo.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

public class DeadLetterTest {
    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("exchange.dlx", "direct", true);
        channel.exchangeDeclare("exchange.normal", "fanout", true);

        HashMap<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 10000);
        map.put("x-dead-letter-exchange", "exchange.dlx");
        map.put("x-dead-letter-routing-key", "routingKey");

        channel.queueDeclare("queue.normal", true,
                false, false, map);
        channel.queueBind("queue.normal", "exchange.normal", "");
        channel.queueDeclare("queue.dlx", true, false,
                false, null);
        channel.queueBind("queue.dlx", "exchange.dlx",
                "routingKey");
        channel.basicPublish("exchange.normal", "",
                MessageProperties.PERSISTENT_TEXT_PLAIN, "dlx".getBytes());

        channel.close();
        connection.close();
    }
}
